<?php

namespace Modules\Admin\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Modules\Admin\Models\BaseModel;
use Illuminate\Support\Facades\Hash;
class Admin extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'username', 'password','uuid','group_id','status'
    ];
	
	protected $hidden = [
        'password', 'remember_token',
    ];
	
	/**
	 * @name  关联权限组表   多对一
	 */
	 public function auth_groups()
    {
        return $this->belongsTo('Modules\Admin\Models\AuthGroup','group_id','id');
    }
	
	/**
	 * @name  状态公共方法
	 */
	protected static function common_status(){
		return BaseModel::common_status();
	}
	/**
	 * @name  添加方法
	 */
	public function admin_create($data,$url){
		$data['created_at'] = date('Y-m-d H:i:s');
		if ($this->insert($data)){
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => config('admin.create_success'),
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.create_error'),
				'msg' => '',
				'url'=>$url
			],201);
		}
	}
	/**
	 * @name  编辑方法
	 */
	public function admin_update($data,$id,$url){
		unset($data['_method']);
		$data['updated_at'] = date('Y-m-d H:i:s');
        if ($this->where('id',$id)->update($data)){
            return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => config('admin.update_success'),
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.update_error'),
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
	/**
	 * @name  调整状态
	 */
	public function admin_status_update($data,$id,$url,$msg = '',$error = ''){
		if($msg == ''){
			$msg = config('admin.status_success');
		}
		if($error == ''){
			$error = config('admin.status_error');
		}
		unset($data['_method']);
        if ($this->where('id',$id)->update($data) !== false){
            return response()->json([
				'status'=>1, 
				'error' => '',
				'msg' => $msg,
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => $error,
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
	/**
	 * @name  删除
	 */
	public function admin_destroy($ids,$url){
        if ($this->destroy($ids)){
            return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => config('admin.delete_success'),
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.delete_error'),
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
	/**
	 * @name  修改密码
	 */
	public function admin_upadte_pwd($data,$id,$url){
		$password = $this->where('id',$id)->value('password');
		if(!Hash::check($data['y_password'],$password)){
			return response()->json([
				'status'=>0,
				'error' => '原密码错误！', 
				'msg' => '',
				'url'=>$url 
			],201);
		}
        if ($this->where('id',$id)->update(['password'=>bcrypt($data['password'])]) !== false){
            return response()->json([
				'status'=>1, 
				'error' => '',
				'msg' => '修改密码成功！',
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => '修改密码失败！',
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
}
